﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;
using MVVM.Core;

namespace ObjectModel.PowerPoint
{
    /// <summary>
    /// A class that updates the state of a dictionary of LinkedObjectCollections in
    /// response to PowerPoint events.
    /// </summary>
    public class LinkedObjectCollectionManager : NotificationBase
    {
        readonly Dictionary<int, LinkedObjectCollection> mLinkedObjectCollections = new Dictionary<int,LinkedObjectCollection>();
        readonly PresentationEvents mEvents;
        LinkedObjectCollection mActiveCollection;
        
        public LinkedObjectCollectionManager(Ppt.Presentation pres)
        {
            mEvents = Globals.PresentationEvents[pres];
            mEvents.SlideDeleted += OnSlideDeleted;
            mEvents.SlideActivated += OnSlideActivated;
        }

        public LinkedObjectCollection ActiveCollection
        {
            get { return mActiveCollection; }

            set
            {
                mActiveCollection = value;
                OnPropertyChanged("ActiveCollection");
            }
        }

        void OnSlideDeleted(object sender, SlideChangedEventArgs e)
        {
            int slideId = e.SlideInfo.Id;
            if (mLinkedObjectCollections.ContainsKey(slideId))
                mLinkedObjectCollections.Remove(slideId);
        }

        void OnSlideActivated(object sender, SlideChangedEventArgs e)
        {
            int slideId = e.SlideInfo.Id;

            if (!mLinkedObjectCollections.ContainsKey(slideId))
                mLinkedObjectCollections.Add(slideId, new LinkedObjectCollection());

            ActiveCollection = mLinkedObjectCollections[slideId];
        }
    }
}
